<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class collection_model extends CI_Model {
	  
   	public function __construct()
    {
        parent::__construct();// a must for constructor
        $this->load->model('tdatabase_model', 'tdatabase');
        $this->load->model('images_model', 'images');
    }
    
 	public function delete($collID)
    {
        $images = $this->tdatabase->get_entry_where('collID', $collID, 'img');
        if (is_array($images)) { 
            foreach ($images as $img) {
                $this->images->delete($img->id);
            }
        }
        $this->tdatabase->remove_entry('id', $collID, 'collections');
    }
        
 	public function add()
    {
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
        if ($this->form_validation->run() == true) { 
            // check to see if the form was filled properly
            $inputs['title'] = $this->input->post('title');;
            $inputs['description'] = $this->input->post('description');;
            $this->tdatabase->insert_entry($inputs, 'collections');
        } else {  // form was not filled properly
            //set the flash data error message if there is one
            return (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
        }
    }
    
    public function update($collID)
    {
    }
        
    public function updateCover($collID)
    {
        $images = $this->tdatabase->get_entry_where('collID', $collID, 'img');
        $last_image = end($images);
        $data['cover'] = $last_image->thumbnail;
        $data['id'] = $collID;
        $this->tdatabase->update_entry($data, 'collections');
    }
}



